﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CalendarApplication
{
    public partial class ucCalendarPallette : UserControl
    {
        ucCalendarContainer _cal;


        //constructor taking datetime and calendar container
        public ucCalendarPallette(DateTime start, ucCalendarContainer Cal)
        {
            InitializeComponent();
            //filling the local variable
            _cal = Cal;
            //calling filldays
            FillDays(start);
        }

        //filling the days below using nested loops
        private void FillDays(DateTime s)
        {
            tableLayoutPanel1.SuspendLayout();
            
            DateTime FirstDay = new DateTime(s.Year, s.Month, 1);
            DateTime fDay = FirstDay;
            int col = GetNumOfWeekDay(FirstDay.DayOfWeek);
            //fill in last month day
            int lmd = 1;
            for (int lmi = (col - 1); lmi > -1; lmi--)
            {
                ucDayTemplate ulm = new ucDayTemplate(fDay.AddDays(-lmd), _cal);
                ulm.Name = fDay.AddDays(-lmd).ToString();
                ulm.BackColor = Color.FromArgb(194, 209, 202);
                ulm.Dock = DockStyle.Fill;
                tableLayoutPanel1.Controls.Add(ulm, lmi, 1);
                lmd++;
            }
            int row = 1;
            for (int i = 1; i < DateTime.DaysInMonth(s.Year, s.Month) + 1; i++, col++)
            {
                if (col > 6)
                {
                    col = 0;
                    row++;
                }
                ucDayTemplate u = new ucDayTemplate(fDay.AddDays(i - 1), _cal);
                u.Name = fDay.AddDays(i - 1).ToString();
                u.Dock = DockStyle.Fill;

                tableLayoutPanel1.Controls.Add(u, col, row);
            }

            int z = 0;
            while (row < 7)
            {
                int x = 0;
                for (x = (col + 1); col < 7; x++, col++)
                {
                    if (col < 7)
                    {
                        ucDayTemplate nm = new ucDayTemplate(fDay.AddDays(DateTime.DaysInMonth(s.Year, s.Month) + z), _cal);
                        nm.Name = fDay.AddDays(DateTime.DaysInMonth(s.Year, s.Month) + z).ToString();
                        nm.BackColor = Color.FromArgb(194, 209, 202);
                        nm.Dock = DockStyle.Fill;
                        z++;
                        tableLayoutPanel1.Controls.Add(nm, col, row);
                    }
                }
                row++;
                col = 0;
            }
            tableLayoutPanel1.ResumeLayout(true);
        }

        private int GetNumOfWeekDay(DayOfWeek dow)
        {
            return (Int32)Enum.Parse(typeof(DayOfWeek), dow.ToString());
        }
    }
}
